<?php

/**
 * @Author: liu
 * @Date:   2018-04-19 16:35:33
 * @Last Modified by:   liu
 * @Last Modified time: 2018-04-26 14:29:56
 * 本方法用于 供应商的报价
 * 
 */
class Gongyingshang
{
    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-04-19T16:40:06+0800
     * @param    [array]                   $item_ids           	[erp 耗材id的数组]
     * @param    [array]                   $suppller_ids        [erp 经销商的id]
     * @param    [Integer]                 $shop_id            	[门店的id]
     * @param    [Integer]                 $is_select_kangzhong [是否检索康众 0 不检索]
     * @return   [type]                                       	[description]
     */
    public static function getPriceStorage($item_ids,$suppller_ids,$shop_id,$is_select_kangzhong)
    {
    	$temp_aray=array();
    	//第一部分
    	$arc=DB::connection('mysql_arc')->table("account_product")->
    		leftJoin("product","product.id","=","account_product.product_id")->
    		leftJoin("ssp_arc_account","ssp_arc_account.id","=","account_product.ssp_id")->
    		leftJoin("account_scs_shop","account_scs_shop.arc_account_id","=","account_product.account_id")->
    		where("account_scs_shop.scs_shop_id",$shop_id)->
    		where("account_product.status",1)->
    		whereIn("ssp_arc_account.erp_supplier_id",$suppller_ids)->
    		where("ssp_arc_account.status",1)->
    		whereIn("product.erp_supply_item",$item_ids)->where("product.status",1)
                ->select("account_product.ssp_id","account_product.product_id","account_product.invoice_amount","product.erp_supply_item","ssp_arc_account.erp_supplier_id")->get();

    	foreach ($arc as $_arc) {
            $tempstoaage=DB::connection('mysql_arc')->table("arc_product_storage")->where("ssp_arc_id",$_arc->ssp_id)->where("product_id",$_arc->product_id)->first();
            $temp_aray[]=array(
                    //"item_id"=>$_arc->product_id,
                    "item_id"=>$_arc->erp_supply_item,  //erp id
                    //"suppller_id"=>$_arc->ssp_id,
                    "suppller_id"=>$_arc->erp_supplier_id,  //erp id
                    "type"=>2,
                    "price"=>$_arc->invoice_amount,//单位分
                    "storage"=>isset($tempstoaage)?$tempstoaage->usable_storage_num:-1,
            );
    	}	
    	//第二部分      康众的 请求
        $kangzhong_arr = [];    //康众查询返回的数据
    	//获取耗材的对应关系
    	$kangzhongItem=array();
        $relation_arr = [];     //康众商品与erp转换数组     康众商品编码为键 ，erp为值
    	$item=DB::connection('mysql_arc')->table("erp_kangzhong_item")
                ->whereIn("erp_supply_item_id",$item_ids)
                ->where("status",1)
                ->select("kangzhong_item_code","erp_supply_item_id")->get();
    	if(sizeof($item)<=0||$is_select_kangzhong==0){
            return $temp_aray;
    	}
    	foreach ($item as $_i) {
            $kangzhongItem[]=$_i->kangzhong_item_code;
            $relation_arr[$_i->kangzhong_item_code] = $_i->erp_supply_item_id;
    	}
        $request = array(
            'method' => "queryAccPriceByAccId",     //查询价格
            'param' => json_encode(array("params"=>array("accId"=>implode(",",$kangzhongItem)))),
            "appKey"=>Config::get('kangzhong.kangzhong_key'),
        );
        $response = Httpful::post(Config::get('kangzhong.kangzhong_url'))
                ->timeoutIn(15)
                ->body($request)
                ->sendsForm()->send();
        $response = json_decode($response);
        $temp_key=Common::private_decode($response->key,Config::get('kangzhong.kangzhong_private_key'));
        $price_info = Common::aes_decode($response->ciphertext,$temp_key);
//Log::info($price_info);
        $price_info = json_decode($price_info, TRUE);
        if($price_info['result'] == '0000' && !empty($price_info['detail'])){
            foreach ($price_info['detail'] as $_detail) {
                $kangzhong_arr[] = [
                    'item_id' => $_detail['accId'],
                    'suppller_id' => '',    //康众的供应商id 暂时为空
                    'type' => 3, //康众     [康众的经销商就是 康众自身]
                    'price' => 100 * $_detail['retailPrice'],     //单位转换为 分
                    'storage' => -1,
                ];
            }
        }
        
     	$s_storalit=DB::connection('mysql_arc')->table("account_scs_shop")->where("scs_shop_id",$shop_id)->first();
     	if($s_storalit&&$s_storalit->kangzhong_code){
            $request = array(
                'method' => "getGoodsStock",    //查询库存
                'param' => json_encode(array("businessCode"=>"1009","params"=>array("accidList"=>implode(",",$kangzhongItem),"storeidList"=>$s_storalit->kangzhong_code))),
                "appKey"=>Config::get('kangzhong.kangzhong_key'),
            );
            $response = Httpful::post(Config::get('kangzhong.kangzhong_url'))
                    ->timeoutIn(15)
                    ->body($request)
                    ->sendsForm()->send();
            $response = json_decode($response);
            
            $temp_key=Common::private_decode($response->key,Config::get('kangzhong.kangzhong_private_key'));
            $info=Common::aes_decode($response->ciphertext,$temp_key);
//            Log::info($info);
            $info = json_decode($info, TRUE);
            if($info['result'] == '0000' && !empty($info['detail']['detailList'])){
                foreach ($kangzhong_arr as $k => $v) {
                    foreach ($info['detail']['detailList'] as $key => $value) {
                        if($v['item_id'] == $value['accId']){
                            $kangzhong_arr[$k]['storage'] = $value["itemList"][0]['amount'];       //库存更新
                            unset($info['detail']['detailList'][$key]);
                        }
                    }
                }
            }
            if(!empty($kangzhong_arr)){     //转换为 erp id
                foreach ($kangzhong_arr as $k => $v) {
                    $kangzhong_arr[$k]['item_id'] = $relation_arr[$v['item_id']];   //返回erp id
                    //$kangzhong_arr[$k]['suppller_id'] = -1;  //康众平台经销商ID 默认返回 -1
                }
            }
     	}
        return array_merge($temp_aray, $kangzhong_arr);
    }



   

}